package com.mpq.controler;

import com.mpq.aspects.DataSourceNames;
import com.mpq.entity.User;
import com.mpq.interfaces.DataSource;
import com.mpq.service.IUserService;
import com.mpq.utils.R;
import com.mpq.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置多数据源
 * @author lqh
 * @since 2018-12-03
 */
@RestController
@RequestMapping("/mpq/dataSource")
public class DataSourceController {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private javax.sql.DataSource dataSource;

    @RequestMapping("getUser")
    public R getUser() {
        User user1 = iUserService.findUserByFirstDb(1L);
        User user2 = iUserService.findUserBySecondDb(1L);
        User user3 = iUserService.findUserByThirdDb(1L);
        User user4 = iUserService.selectById(1);
        Map<String,Object> map = new HashMap<>();
        map.put("user1",user1);
        map.put("user2",user2);
        map.put("user3",user3);
        map.put("user4",user4);
        redisUtil.lSet("user1",user1);
        return R.ok().put("result", map);
    }
    @RequestMapping("getDataUser")
    @DataSource(name = DataSourceNames.THIRD)
    public R getDataUser() {
        User user1 = iUserService.selectById(1L);
        User user2 = iUserService.selectById(1L);
        User user3 = iUserService.selectById(1);
        Map<String,Object> map = new HashMap<>();
        map.put("user1",user1);
        map.put("user2",user2);
        map.put("user3",user3);
        return R.ok().put("result", map);
    }
    @RequestMapping("getDataSource")
    public R getDataSource(){
        return R.ok().put("result", dataSource.getClass());
    }
}
